package com.west_traveler.ximalayafm;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.west_traveler.fragment.DownloadFragment;
import com.west_traveler.fragment.FindFragment;
import com.west_traveler.fragment.MineFragment;
import com.west_traveler.fragment.SubScribeFragment;
import com.west_traveler.play_service.PlayService;

import java.util.LinkedList;


public class MainActivity extends AppCompatActivity {

    private FindFragment findFragment;
    private SubScribeFragment subScribeFragment;
    private DownloadFragment downloadFragment;
    private MineFragment mineFragment;
    private RadioGroup radioGroup;
    private FragmentManager fragmentManager;
    private LinkedList<Fragment> fragments;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getNetPermission();

        //初始化控件
        initViews();
        //关于RadioGroup
        aboutRadioGroup();
        //获取fragmentManger
        fragmentManager=getSupportFragmentManager();
        // 界面第一次启动，设置第一个RadioButton为选中
       currentFragment = fragments.get(0);
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);


    }

    //关于RadioGroup
    private void aboutRadioGroup() {
        //给RadioGroup设置监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                // 遍历RadioGroup的子控件，找到被选中的那一个，使用相应的Fragment实例替换占位的容器控件
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton rb = (RadioButton) radioGroup.getChildAt(i);
                    if (rb.isChecked()) {
                        changeContainerWidget(i);
                        break;
                    }
                }
            }
        });

    }
    /**
     * 修改占位的容器控件
     *
     * @param i
     */
    private void changeContainerWidget(int i) {
        FragmentTransaction beginTransaction = fragmentManager
                .beginTransaction();
        // 1、隐藏当前的
        beginTransaction.hide(currentFragment);

        // 2、判断选中的RadioButton对应的Fragment是否曾经附着在容器控件上
        Fragment nowFrament = fragments.get(i);
        // ①附着上了，就显示
        // ②没有附着，先附着，再显示
        if (!nowFrament.isAdded()) {
            beginTransaction.add(R.id.container_main_id, nowFrament);
        }
        beginTransaction.show(nowFrament);

        // 3、将目前选中Fragment赋值给当前currentFragment
        currentFragment = nowFrament;

        // 4、commit
        beginTransaction.commit();

    }

    private void getNetPermission() {

    }


    //初始化控件
    private void initViews() {
        //准备一个集合存放 fragment
        fragments = new LinkedList<>();
        radioGroup = ((RadioGroup) findViewById(R.id.relative_layout_main_id));
        findFragment = new FindFragment();
        subScribeFragment = new SubScribeFragment();
        downloadFragment = new DownloadFragment();
        mineFragment = new MineFragment();
        fragments.add(findFragment);
        fragments.add(subScribeFragment);
        fragments.add(downloadFragment);
        fragments.add(mineFragment);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(getApplicationContext(), PlayService.class));
    }
}
